package soar.test.log.log4j2;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class App
{
    public static void main(String[] args)
    {
        new App().run();
    }

    private static final Logger logger = LoggerFactory.getLogger(App.class);

    private void run()
    {
        String split = "========================================";
        System.out.println(split);

        logger.info("App");
        System.out.println(split);

        new Worker().run();
        System.out.println(split);

        benchmark();
        System.out.println(split);
    }

    private void benchmark()
    {
        final Logger logger_quick = LoggerFactory.getLogger("quick");
        final Logger logger_slow = LoggerFactory.getLogger("slow");

        Instant begin;
        Instant end;
        long dur;
        int num = 100000;

        begin = Instant.now();
        for (int i = 0; i < num; i++)
        {
            logger_quick.info("quick {}", i + 1);
        }
        end = Instant.now();
        dur = end.toEpochMilli() - begin.toEpochMilli();
        System.out.println(
                String.format("quick: %d ms, %d/s", dur, num * 1000 / dur));

        begin = Instant.now();
        for (int i = 0; i < 100000; i++)
        {
            logger_slow.info("slow {}", i + 1);
        }
        end = Instant.now();
        dur = end.toEpochMilli() - begin.toEpochMilli();
        System.out.println(
                String.format("slow: %d ms, %d/s", dur, num * 1000 / dur));
    }
}

class Worker
{
    private static final Logger logger = LoggerFactory.getLogger(Worker.class);

    public void run()
    {
        logger.info("Worker");
        logger.debug("debug");
        logger.error("error");
        logger.info("你好！{} str:{} length:{} date:({}) %s {}", "{}", "hello",
                100,
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),
                null);

        byte[] buf = new byte[] { 0x12, 0x55, (byte) 0xee, (byte) 0xff };
        logger.info("{} {} {} {}", String.format("%x", buf[0]),
                String.format("%X", buf[1]), String.format("%X", buf[2]),
                String.format("%X", buf[3]));
        StringBuffer strbuf = new StringBuffer();
        for (int i = 0; i < buf.length; i++)
        {
            strbuf.append(String.format("%x", buf[i]));
        }
        logger.info("{}", strbuf);
    }
}
